package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.ProjPaymentLogic;
import cn.topevery.garden.entity.dto.ProjBidCudDTO;
import cn.topevery.garden.entity.dto.ProjPaymentCudDTO;
import cn.topevery.garden.entity.dto.ProjPaymentDTO;
import cn.topevery.garden.entity.query.ProjPaymentQuery;
import cn.topevery.garden.entity.vo.ProjPaymentVO;
import cn.topevery.garden.framework.Database;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 项目支付表
 *
 * @author code-generate
 * @date 2022-11-02
 */
@Api(tags = "工程项目-项目支付表")
@RestController
@RequestMapping("/projPayment")
public class ProjPaymentController extends BaseController {

    @Autowired
    ProjPaymentLogic projPaymentLogic;


    @ApiOperation("新增/编辑/删除 项目支付表信息")
    @PostMapping("/saveOrUpdateOrDelete")
    public JsonResult<Boolean> saveOrUpdateOrDelete(@Valid @RequestBody ProjPaymentCudDTO cudDto) {
        Database.transactionScope(() -> {
            projPaymentLogic.saveOrUpdateOrDelete(getUser().userId, cudDto);
        });
        return JsonResult.ok(true);
    }

    /**
     * 获取项目支付表列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取项目支付表列表")
    @PostMapping("/list")
    public JsonResult<List<ProjPaymentVO>> list(@RequestBody ProjPaymentQuery query) {
        return JsonResult.ok(projPaymentLogic.list(query));
    }

    /**
     * 获取项目支付表分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取项目支付表分页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<ProjPaymentVO>> listPage(ProjPaymentQuery query) {
        return JsonResult.ok(projPaymentLogic.listPage(query));
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        projPaymentLogic.delete(getUser().userId, ids);
        return JsonResult.ok(true);
    }

    /**
     * 新增项目支付表信息
     *
     * @param dto
     * @return
     */
    @ApiOperation("新增项目支付表信息")
    @PostMapping("/insert")
    public JsonResult<Boolean> insert(@Valid @RequestBody ProjPaymentDTO dto) {
        projPaymentLogic.save(getUser().userId, dto);
        return JsonResult.ok(true);
    }

    @ApiOperation("根据guid获取详情")
    @GetMapping("/getByGuid")
    public JsonResult<ProjPaymentVO> getByGuid(@RequestParam(value = "guid") String guid) {
        ProjPaymentVO vo = new ProjPaymentVO();
//        DataFillHelper.readToTarget(projPaymentLogic.getEntityByDataId(guid), vo);
        return JsonResult.ok(vo);
    }

    @ApiOperation("导出项目支付表信息")
    @GetMapping("/export")
    public void export(ProjPaymentQuery query, HttpServletResponse response) {
        List<ProjPaymentVO> list = projPaymentLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "项目支付表", "sheet1", ProjPaymentVO.class, "项目支付表", response);
    }
}
